package leetcode.code0599;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;

public class Solution implements IDebug {

	public String[] findRestaurant(String[] list1, String[] list2) {
		Map<String, Integer> map = new HashMap<>();
		for (int i = 0; i < list1.length; i++) {
			map.put(list1[i], i);
		}
		int min = Integer.MAX_VALUE;
		List<String> ans = new ArrayList<>();
		for (int i = 0; i < list2.length; i++) {
			String cur = list2[i];
			Integer index = map.getOrDefault(cur, -1);
			if (index != -1) {
				int sum = index + i;
				if (sum < min) {
					min = sum;
					ans = new ArrayList<>();
					ans.add(cur);
				} else if (sum == min) {
					ans.add(cur);
				}
			}
		}
		return ans.toArray(new String[ans.size()]);
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
